/// @tags:
#include <cstdio>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef unsigned long long ULL;

constexpr int maxn = 103;
constexpr ULL full = (1ULL << 63) - 1;

struct Matrix {
  static int n;
  ULL a[maxn][maxn];

  inline Matrix(ULL x = 0) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        a[i][j] = i == j ? x : 0;
      }
    }
  }

  inline ULL *operator[](int index) { return a[index]; }
  inline const ULL *operator[](int index) const { return a[index]; }

  inline Matrix operator*(const Matrix &rhs) const {
    Matrix res;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) res[i][j] |= a[i][k] & rhs[k][j];
      }
    }
    return res;
  }

  inline Matrix operator^(int exp) const {
    Matrix res(full), base(*this);
    for (; exp; exp >>= 1, base = base * base)
      if (exp & 1) res = res * base;
    return res;
  }
};

int Matrix::n;

inline int main() {
  int n, k;
  cin >> n >> k;
  Matrix::n = k;
  Matrix ini, trans;
  for (int i = 0; i < k; ++i) {
    cin >> ini[0][i];
  }
  for (int i = 0; i < k; ++i) {
    cin >> trans[i][k - 1];
  }
  for (int i = 1; i < k; ++i) {
    trans[i][i - 1] = full;
  }
  cout << (ini * (trans ^ n))[0][0];
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5678 [GZOI2017] 河神.in", "r", stdin);
  freopen("P5678 [GZOI2017] 河神.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
